﻿using System;
using System.IO;
using System.Text;
using LingDong.ConfigProvider;
using NLog;

namespace LingDong.DataProvider
{
    /// <summary>
    /// SogouT2 is an internet corpus provided by sogou lab
    /// The big difference between SogouT2 and SogouT is T2 only contains HTML text
    /// Each do data contains five lines, the format is: 
    /// <doc>
    /// <url>Doc URL</url>
    /// </docid>Doc ID</docid>
    /// <content>Doc Content, without HTML tags, text only</content>
    /// </doc>
    /// </summary>
    public class SogouT2DataReader : IDataReader
    {
        #region Contructors

        public SogouT2DataReader()
        {
            string path = ConfigDataProvider.Default.SogouT2DataPath;
            string encode = ConfigDataProvider.Default.SogouT2DataEncode;
            try
            {
                reader = new StreamReader(path, Encoding.GetEncoding(encode));
            }
            catch (Exception ex)
            {
                Logger logger = LogManager.GetCurrentClassLogger();
                logger.Error("Cannot open sogoug data file: {0}", ex.Message);
                throw;
            }
        }

        #endregion
        

        #region Public Members

        /// <summary>
        /// Get next html data in sogou data file
        /// </summary>
        /// <returns></returns>
        public DataEntity GetNextData()
        {
            if (reader.EndOfStream)
                return null;

            DataEntity data = new DataEntity();
            try
            {
                string docBegin = reader.ReadLine();
                string url = reader.ReadLine();
                string docid = reader.ReadLine();
                string content = reader.ReadLine();
                string docEnd = reader.ReadLine();

                data.Url = url.Substring(5, url.Length - 11);
                data.DocID = docid.Substring(8, docid.Length - 16);
                data.Content = content.Substring(9, content.Length - 19);
            }
            catch (Exception ex)
            {
                Logger logger = LogManager.GetCurrentClassLogger();
                logger.Error("Unexpeted format in sogouT2 data file: {0}", ex.Message);
                throw;
            }
            return data;
        }

        /// <summary>
        /// Return if all data in sogou data file has been read
        /// </summary>
        /// <returns></returns>
        public bool IsAllDataRead()
        {
            return reader.EndOfStream;
        }

        /// <summary>
        /// Return true if the data only contains text
        /// false if the data contains HTML tags
        /// </summary>
        /// <returns></returns>
        public bool IsDataContainsOnlyText()
        {
            return true;
        }

        #endregion

        
        #region Private Members

        private StreamReader reader;

        #endregion
        

        #region Disposable related

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            // protect from being called multiple times.
            if (disposed)
            {
                return;
            }

            if (disposing)
            {
                // clean up all managed resources.
                if (reader != null)
                {
                    reader.Dispose();
                }
            }

            disposed = true;
        }

        private bool disposed = false;

        #endregion
    }
}
